#       report-paging.py
#       
#       Copyright 2008 Edwood Ocasio <edwood.ocasio@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
#!/usr/bin/env python

# Uses the patched reporting api module
import reporting
#########################################
import sys
import getpass

def report(report_name, report_date=None, page=None):
	""" Retrieves named report for date specified. 
	
		Available report_names: 
		'accounts','disk_space','summary','activity', 'email_clients'
		
		report_date should be in the format YYYY-MM-DD
		
		If report_date is not specified it returns the latest available 
		report.
		
		If page is None all report pages are retrieved.
		
	"""
	
	import re
	import time
	
	global adminemail
	global domain	
	
	report_runner = reporting.ReportRunner()		
	
  	report_runner.admin_email = raw_input("Administrator Username: ")
  	report_runner.admin_password =getpass.getpass("Administrator Password: ")
  	domain=report_runner.admin_email.split("@")[1]
  	report_runner.domain = domain
  	rdate=time.strftime("%a-%d-%b-%Y-%H-%M",time.localtime())
	#file=open("usuarios-" + postfix + ".gae",'a')
	
	if report_date==None:
		out_file_name=report_name + "_" + rdate + "_" + domain + ".csv"
	else:
		out_file_name=report_name + "_" + report_date + "_" + domain +  ".csv"
	#out_file_name=None
	
  	# Date should be in the format YYYY-MM-DD 
  	if (not report_runner.admin_email or not report_runner.admin_password or \
  	not report_name or (report_date is not None and not \
  	re.compile('\d{4}-\d{2}-\d{2}').match(report_date))):
  		print _("Report could not be retrieved")
  	else:
 
  		if page != None:
  			report_runner.RunReport(report_name, report_date, out_file_name,page_requested=page)
  		else:
  			# Retrieve All Report Pages
  			EndOfReport=False
  			numpage=0
  			while not EndOfReport:
  				numpage = numpage + 1 
  				print "Page ",numpage
				response=report_runner.RunReport(report_name, report_date, out_file_name,page_requested=numpage)
				EndOfReport= (response == "End-Of-Report") 				
  				
  		for line in open(out_file_name,'r'):
  			print line
  		
  		print "This report was saved in file '",out_file_name,"'"

def main():
		
	global adminemail
	global domain	
	
	adminemail=""
	domain=""
	
	print "Example 1: Specific page of a report (10,000 users/page)"
	print "report('accounts',page=1)"
	report('accounts',page=1)
	print 
	print "Example 2: All report pages (10,000 users/page)"
	print "report('accounts')"
	report('accounts')
	
if __name__ == '__main__':
  main()
 
